<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests\UserInfo;
use App\Http\Controllers\Controller;
use App\Models\Wallet;
use App\Models\MoneyLog;
use App\Models\Withdraw;
use App\Models\Order;
use App\Models\User;
use DB;

class CenterController extends Controller
{
    //个人中心
    public function index()
    {
    	echo '个人中心';
    }
    //邀请好友
    public function invite(Request $request)
    {
        $user = $request->user;
        
        $register_code = $user->register_code;
        if($register_code){
            $invite_url = url('/register').'/'.$register_code;
            return $this->ajax_data(compact('invite_url'));
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    //退出登录
    public function logout(Request $request)
    {
        $res = $request->session()->forget('user_id');
        if($res){
            return $this->ajax_success('退出成功');
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    // 我的钱包-我的资产(总资产、可用余额、累计赚钱)
    public function my_wallet(Request $request)
    {
        $user = $request->user;
        $wallet = new Wallet();
        $fund_data = $wallet->get_wallet_data($user->id);
        if($fund_data){
            return $this->ajax_data($fund_data);
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    // 我的钱包-资金明细(日期、类型、交易金额、可用余额)
    public function fund_detail(Request $request)
    {
        $user = $request->user;
        $MoneyLog = new MoneyLog();
        $fund_detail = $MoneyLog->get_user_moneylog($request);
        if($fund_detail->isNotEmpty()){
            return $this->ajax_data($fund_detail);
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    //申请提现
    public function apply_withdraw(Request $request)
    {   
        $user = $request->user;
        $apply_money = $request->apply_money;//提现金额
        $apply_type = $request->type;//提现方式1支付宝2微信
        if($apply_money>$wallet->money){
            return $this->ajax_error('可用余额不足');
        }
        if($apply_type == 1 && isset($user->alipay)){
            return $this->ajax_error('请先完善支付宝账号信息');
        }
        if($apply_type == 2 && isset($user->wechat)){
            return $this->ajax_error('请先完善微信账号信息');
        }
        $wallet = Wallet::where('user_id',$user->id)->first();
        
        
        DB::beginTransaction();
        try {
            $withdraw = new Withdraw();
            $withdraw->user_id = $user->id;
            $withdraw->money = $request->apply_money;
            $withdraw->type = $request->type;
            $withdraw->status = 1;
            $withdraw->save();

            $wallet->money = $wallet->money - $apply_money;
            $wallet->save();
            DB::commit();
            return $this->ajax_success('提现成功,1-3天到账,请耐心等待');
        } catch (QueryException $ex) {
            DB::rollback();
            return $this->ajax_error('操作失败');
        }

    }
    // 我的钱包-提现记录(金额、时间、提现到--全部、成功、审核中、失败)
    public function withdraw(Request $request)
    {
        $user_id = $request->user->id;
        $status = $request->status;
        $withdraw = Withdraw::when($status, function ($query,$status){
            return $query->where('status',$status);
        }, function ($query)  use ($user_id) {
            return $query->whereUser_id($user_id);
        })
        ->get();       
        if($withdraw->isNotEmpty()){
            return $this->ajax_data($withdraw);
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    // 我的任务(进行中、审核中、已成功、已失败)
    public function my_task()
    {
        $user_id = $request->user->id;
        $status = $request->status;
        $my_task = Order::when($status, function ($query,$status){
            return $query->where('status',$status);
        }, function ($query)  use ($user_id) {
            return $query->whereUser_id($user_id);
        })
        ->with('task')->get(); 
        if($my_task->isNotEmpty()){
            return $this->ajax_data($my_task);
        }else{
            return $this->ajax_error('暂无数据');
        }
    }
    // 我的邀请(我要推荐、推荐记录)
    public function my_invite(Request $request)
    {
        $user_id = $request->user->id;
        $user = new User();
        $sub_user = $user->_subUsers($user_id);
        if($sub_user->isNotEmpty()){
            return $this->ajax_data($sub_user);
        }else{
            return $this->ajax_error('暂无数据');
        }
        
    }
    // 个人资料(头像、手机、真实姓名、身份证号、昵称、微信账号、支付宝账号)
    public function my_info(Request $request)
    {
        $user = $request->user;
        if($user){
            return $this->ajax_data($user);
        }else{
            return $this->ajax_error('请重新登录');
        }
    }
    // 编辑个人信息
    public function edit_info(UserInfo $request)
    {
        $user = $request->user;
        $user->name = $request->name;
        $user->real_name = $request->real_name;
        $user->alipay = $request->alipay;
        $user->wechat = $request->wechat;
        $res = $user->save();
        if($res){
            return $this->ajax_success('修改成功');
        }else{
            return $this->ajax_error('修改失败');
        }
    }
}
